home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Gamer Resource Kit / Hardcore Gamer Resource Kit - Disc 2.iso / Utils / UNIX / UNZIP520 / WINGUI / MAKEDIR.C < prev    next >
C/C++ Source or Header  |  1996-02-06  |  7KB  |  273 lines

  1. #include <windows.h>
  2. #include <ctype.h>
  3. #include <dos.h>
  4. #ifndef WIN32
  5. #   ifndef __BORLANDC__
  6. #      include <direct.h>
  7. #   else
  8. #      include <dir.h>
  9. #   endif /* !__BORLANDC__ */
  10. #else
  11. #   ifndef __BORLANDC__
  12. #      include <direct.h>
  13. #   else
  14. #      include <dir.h>
  15. #   endif /* !__BORLANDC__ */
  16. #endif /* !WIN32 */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "wingui\wizunzip.h"
  20. #include "wingui\helpids.h"
  21.  
  22. #ifndef TRUE
  23. #define TRUE 1
  24. #endif
  25.  
  26. #ifndef FALSE
  27. #define FALSE 0
  28. #endif
  29.  
  30. #ifndef WIZUNZIP_MAX_PATH
  31. #define WIZUNZIP_MAX_PATH 128
  32. #endif
  33.  
  34. /*
  35.  * Function: BOOL MakeDirectory(char *path, BOOL fileAttached)
  36.  *
  37.  * Return:   TRUE if successful
  38.  *           FALSE if unsuccessful
  39.  *
  40.  * Creates directories specified by path. These can be any level of
  41.  * previously non-existent directories, up to WIZUNZIP_MAX_PATH.
  42.  *
  43.  * First checks to see if a drive is designated, and if so, if the
  44.  * drive exists. If drive doesn't exist, returns FALSE.
  45.  *
  46.  * Then successively creates the directories (assuming they don't exist).
  47.  *
  48.  * Placed in the public domain by Mike White, 1995
  49.  *
  50.  */
  51.  
  52. BOOL MakeDirectory(char *path, BOOL fileAttached);
  53.  
  54. BOOL MakeDirectory(char *path, BOOL fileAttached)
  55. {
  56. char tempPath[WIZUNZIP_MAX_PATH], tempChar;
  57. BOOL slash = FALSE;
  58. #ifdef WIN32
  59. DWORD attrib;
  60. unsigned int dReturn;
  61. #else
  62. unsigned int attrib;
  63. WORD dReturn;
  64. #endif
  65. int i;
  66.  
  67. if (path[0] == '\0')
  68.    return TRUE; /* Don't need to make a directory */
  69.  
  70. lstrcpy(tempPath, path);
  71. if (tempPath[1] == ':') /* check if drive included */
  72.     {
  73.     if (lstrlen(tempPath) < 4)
  74.         return TRUE; /* specified only a drive and a root directory */
  75. #ifndef WIN32
  76.     dReturn = GetDriveType(toupper(tempPath[0]) - 'A');
  77. #else
  78.     tempPath[2] = '\\';
  79.     tempPath[3] = '\0';
  80.     dReturn = GetDriveType(tempPath);
  81.     lstrcpy(tempPath, path);
  82. #endif
  83.  
  84.     if (dReturn == 0) /* drive type can't be determined */
  85.         return FALSE;
  86. #ifdef WIN32
  87.     if (dReturn == 1) /* root directory doesn't exist */
  88.         return FALSE;
  89. #endif
  90.     }
  91.  
  92. /* Is this a valid path already? If so, return TRUE */
  93. /* There appears to be a bug (feature?) in NT 3.51 (and perhaps other
  94.    versions as well) that allows you to create a path the first time for a
  95.    given application, but erroneously reports that the path exists, if
  96.    another application deletes the directories, hence the following code
  97.    has been commented out until such time as a fix is figured out.
  98.  
  99. #ifndef WIN32
  100. _dos_getfileattr(tempPath, &attrib);
  101. if (attrib & _A_SUBDIR)
  102.    {
  103.    return TRUE;
  104.    }
  105. #else
  106. attrib = GetFileAttributes(tempPath);
  107. if (attrib & FILE_ATTRIBUTE_DIRECTORY)
  108.    {
  109.    return TRUE;
  110.    }
  111. #endif
  112. */
  113.  
  114. if (tempPath[1] == ':')
  115.    for (i = 3; i < lstrlen(path); i++)
  116.       {
  117.       if (tempPath[i] == '\\')
  118.          {
  119.          tempChar = tempPath[i];
  120.          tempPath[i] = '\0';
  121.          mkdir(tempPath); /* We don't care what the return value is, if it
  122.                              already exists, we'll get an error. Check later
  123.                              on.
  124.                           */
  125.          tempPath[i] = tempChar;
  126.          slash = TRUE;
  127.          }
  128.       }
  129. else
  130.    for (i = 1; i < lstrlen(path); i++)
  131.       {
  132.       if (tempPath[i] == '\\')
  133.          {
  134.          tempChar = tempPath[i];
  135.          tempPath[i] = '\0';
  136.          mkdir(tempPath); /* We don't care what the return value is, if it
  137.                              already exists, we'll get an error. Check later
  138.                              on.
  139.                           */
  140.          tempPath[i] = tempChar;
  141.          slash = TRUE;
  142.          }
  143.       }
  144. if (slash && !fileAttached)
  145.     mkdir(tempPath);
  146. else
  147.     if (!fileAttached)
  148.        mkdir(tempPath);
  149.  
  150.  
  151. if (fileAttached)
  152.    {
  153.    char *ptr;
  154.    if ((ptr = strrchr(tempPath, '\\')) != NULL)
  155.       *ptr = '\0';
  156.    }
  157.  
  158. /* Now, do we have a valid path? */
  159. #ifndef WIN32
  160. _dos_getfileattr(tempPath, &attrib);
  161. if (attrib & _A_SUBDIR)
  162.    return TRUE;
  163. #else
  164. attrib = GetFileAttributes(tempPath);
  165. if (attrib & FILE_ATTRIBUTE_DIRECTORY)
  166.    return TRUE;
  167. #endif
  168.  
  169. /* Still no valid path, can't create it. return false */
  170. return FALSE;
  171. }
  172.  
  173.  
  174. /****************************************************************************
  175.  
  176.     FUNCTION: MakeDirProc(HWND, unsigned, WPARAM, LPARAM)
  177.  
  178.     PURPOSE:  Processes messages for "Make Directory" dialog box
  179.  
  180.     MESSAGES:
  181.  
  182.     WM_INITDIALOG - initialize dialog box
  183.     WM_COMMAND    - Input received
  184.  
  185. ****************************************************************************/
  186.  
  187. #ifdef __BORLANDC__
  188. #pragma warn -par
  189. #endif
  190. BOOL WINAPI
  191. MakeDirProc(HWND hDlg, WORD wMessage, WPARAM wParam, LPARAM lParam)
  192. {
  193. static HWND hSelect, hMakeDir;
  194. char tempPath[128];
  195. int nPatternLength;   /* length of data in pattern edit window */
  196.  
  197.  
  198.    switch (wMessage) {
  199.    case WM_INITDIALOG:
  200.       hSelect = GetDlgItem(hDlg, IDOK);
  201.       hMakeDir = GetDlgItem(hDlg, IDM_MAKEDIR_PATH);
  202.       SetDlgItemText(hDlg, IDM_CURRENT_PATH,lpumb->szUnzipToDirName);
  203.       CenterDialog(GetParent(hDlg), hDlg);
  204.       return TRUE;
  205.    case WM_COMMAND:
  206.       switch (LOWORD(wParam))
  207.         {
  208.         case IDM_MAKEDIR_PATH:
  209.             if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE)
  210.                {
  211.                if ((nPatternLength = GetWindowTextLength(hMakeDir))==1)
  212.                   {
  213.          /* Enable or disable buttons depending on fullness of
  214.           * "Suffix" box.
  215.           */
  216.                   BOOL fButtonState = TRUE ;
  217.                   WinAssert(hSelect);
  218.                   EnableWindow(hSelect, fButtonState);
  219.                   }
  220.                if (nPatternLength == 0)
  221.                   {
  222.                   BOOL fButtonState = FALSE;
  223.                   WinAssert(hSelect);
  224.                   EnableWindow(hSelect, fButtonState);
  225.                   }
  226.                }
  227.             break;
  228.  
  229.       case IDOK: /* Make Directory */
  230.          GetDlgItemText(hDlg, IDM_MAKEDIR_PATH, tempPath, WIZUNZIP_MAX_PATH);
  231.          if (tempPath[lstrlen(tempPath) - 1] == '\\')
  232.             tempPath[lstrlen(tempPath) - 1] = '\0'; /* strip trailing backslash */
  233.          if (strchr(tempPath, '\\') == NULL)
  234.             {
  235.             char sz[WIZUNZIP_MAX_PATH];
  236.             lstrcpy(sz, lpumb->szUnzipToDirName);
  237.             strcat(sz, "\\");
  238.             strcat(sz, tempPath);
  239.             lstrcpy(tempPath, sz);
  240.             }
  241.          if (!MakeDirectory(tempPath, FALSE))
  242.             {
  243.             char tStr[WIZUNZIP_MAX_PATH];
  244.             sprintf(tStr, "Could not create %s", tempPath);
  245.             MessageBox(hDlg, tStr, NULL, MB_OK | MB_ICONSTOP);
  246.             }
  247.          else
  248.             {
  249.             char tStr[WIZUNZIP_MAX_PATH];
  250.             sprintf(tStr, "Created directory: %s\n", tempPath);
  251.             win_fprintf(stdout, strlen(tStr), tStr);
  252.             }
  253.          EndDialog(hDlg, wParam);
  254.          break;
  255.  
  256.       case IDCANCEL:
  257.          EndDialog(hDlg, wParam);
  258.          break;
  259.       case IDM_MAKEDIR_HELP:
  260.             WinHelp(hDlg,szHelpFileName,HELP_CONTEXT, (DWORD)(HELPID_MAKEDIR_HELP));
  261.       }
  262.       return TRUE;
  263.    case WM_CLOSE:
  264.       DestroyWindow(hDlg);
  265.       return TRUE;
  266.    }
  267.    return FALSE;
  268. }
  269. #ifdef __BORLANDC__
  270. #pragma warn .par
  271. #endif
  272.  
  273.